/* 
 * File:   ProjectorTableModel.cpp
 * Author: longtran
 * 
 * Created on March 15, 2011, 5:21 PM
 */

#include "ProjectorTableModel.h"
#include "projectordata.h"

#include <QtCore/QSize>

namespace Projector
{

ProjectorTableModel* ProjectorTableModel::sInstance = 0;
QMutex* ProjectorTableModel::sMutex = new QMutex();

ProjectorTableModel::ProjectorTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    
}

ProjectorTableModel::~ProjectorTableModel()
{
    
}

int ProjectorTableModel::rowCount( const QModelIndex& /*parent*/) const
{
    return ProjectorData::GetInstance()->GetProjectors()->count();
}

int ProjectorTableModel::columnCount(const QModelIndex& /*parent*/) const
{
    return COLUMNDATA_Count;
}

QVariant ProjectorTableModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid() || role != Qt::DisplayRole )
    {
        return QVariant();
    }

    Projector const* proj = ProjectorData::GetInstance()->GetProjector(index.row());

    switch( index.column() )
    {
        case COLUMNDATA_ProjName:
            return QVariant(proj->GetName());
            break;
        case COLUMNDATA_PowerState:
            return QVariant(static_cast<int>(proj->GetPowerState()));
            break;
        case COLUMNDATA_InputState:
            return QVariant(static_cast<int>(proj->GetInputState()));
            break;
        default:
            return QVariant();
            break;
    }

    return QVariant();
}

QVariant ProjectorTableModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
{
    /*
    if (role == Qt::SizeHintRole)
    {
         return QSize(50, 50);
    }
     * */
    if ( role == Qt::DisplayRole)
    {
        switch( section )
        {
            case COLUMNDATA_ProjName:
                return QVariant( "Projector");
                break;
            case COLUMNDATA_PowerState:
                return QVariant( "Power" );
                break;
            case COLUMNDATA_InputState:
                return QVariant( "Input" );
                break;
            default:
                return QVariant();
                break;
        }
    }

    return QVariant();
}

ProjectorTableModel* ProjectorTableModel::GetInstance()
{
    sMutex->lock();
    if(!sInstance)
    {
        sInstance = new ProjectorTableModel();
    }
    sMutex->unlock();
    return sInstance;
}

}
